package com.cv.platform.service.comment;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.cv.framework.common.pojo.PageResult;
import com.cv.framework.common.utils.object.BeanUtils;
import com.cv.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.cv.framework.security.core.utils.SecurityFrameworkUtil;
import com.cv.infra.api.file.FileApi;
import com.cv.platform.controller.platform.comment.vo.reply.CvCommentReplyReqVO;
import com.cv.platform.controller.platform.comment.vo.reply.CvCommentReplyRespVO;
import com.cv.platform.controller.platform.comment.vo.reply.CvCommentReplySaveReqVO;
import com.cv.platform.dal.dataobject.comment.CvCommentReplyDO;
import com.cv.platform.dal.mapper.comment.CvCommentReplyMapper;
import com.cv.platform.service.video.CvVideoService;
import com.cv.system.api.user.UserApi;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Optional;

/**
 * 视频评论回复业务层接口实现类
 *
 * @author Charles_XDXD
 */
@Service
public class CvCommentReplyServiceImpl implements CvCommentReplyService {

    @Resource
    private CvCommentReplyMapper cvCommentReplyMapper;

    @Resource
    private CvCommentReplyLikeService cvCommentReplyLikeService;

    @Resource
    private CvCommentReplyDislikeService cvCommentReplyDislikeService;

    @Resource
    private CvVideoService cvVideoService;

    @Resource
    private CvCommentService cvCommentService;

    @DubboReference
    private FileApi fileApi;

    @DubboReference
    private UserApi userApi;

    @Override
    public void createCommentReply(CvCommentReplySaveReqVO reqVO) {
        CvCommentReplyDO cvCommentReplyDO = BeanUtils.toBean(reqVO, CvCommentReplyDO.class);
        cvCommentReplyDO.setReplyUserId(SecurityFrameworkUtil.getLoginUserId());
        cvCommentReplyMapper.insert(cvCommentReplyDO);
        cvVideoService.addVideoCommentCount(cvCommentService.getVideoIdByCommentId(reqVO.getCommentId()));
    }

    @Override
    public PageResult<CvCommentReplyRespVO> getCommentReplyPage(CvCommentReplyReqVO reqVO) {
        PageResult<CvCommentReplyRespVO> pageResult = cvCommentReplyMapper.selectPage(reqVO);

        if (CollUtil.isEmpty(pageResult.getList())) {
            return PageResult.empty();
        }

        pageResult.getList().forEach(item -> {
            item.setNickname(userApi.getNicknameById(item.getUserId()));
            item.setAvatar(userApi.getAvatarById(item.getUserId()));
            item.setAvatarUrl(fileApi.getUrl(item.getAvatar()));
            item.setTargetNickname(userApi.getNicknameById(item.getTargetUserId()));
            item.setIsLike(cvCommentReplyLikeService.getCommentReplyLikeStatus(item.getId()));
            item.setIsDislike(cvCommentReplyDislikeService.getCommentReplyDislikeStatus(item.getId()));
            item.setLikeCount(cvCommentReplyLikeService.getCommentReplyLikeCount(item.getId()));
            item.setDislikeCount(cvCommentReplyDislikeService.getCommentReplyDislikeCount(item.getId()));
        });

        return pageResult;
    }

    @Override
    public void deleteAllCommentReply(Long userId) {
        if (ObjUtil.isNotEmpty(userId)) {
            LambdaQueryWrapperX<CvCommentReplyDO> wrapper = new LambdaQueryWrapperX<CvCommentReplyDO>()
                    .eq(CvCommentReplyDO::getReplyUserId, userId);

            cvCommentReplyMapper.delete(wrapper);
        }
    }

    @Override
    public String getCommentReplyContent(Long replyId) {
        if (ObjUtil.isNotEmpty(replyId)) {
            return Optional.ofNullable(cvCommentReplyMapper.selectById(replyId))
                    .orElse(new CvCommentReplyDO().setContent(StrUtil.EMPTY))
                    .getContent();
        }
        return StrUtil.EMPTY;
    }

    @Override
    public void deleteCommentReply(Long replyId) {
        cvCommentReplyMapper.deleteById(replyId);
    }

    @Override
    public Long getSenderIdByReplyId(Long replyId) {
        if (ObjUtil.isNotEmpty(replyId)) {
            return Optional.ofNullable(cvCommentReplyMapper.selectById(replyId))
                    .orElse(new CvCommentReplyDO().setReplyUserId(null))
                    .getReplyUserId();
        }
        return null;
    }

}
